#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class PageControlListProvider : CY.GFive.Core.Providers.DALProvider.IPageControlListProvider
	{
		#region Sql
		private static readonly string SqlSelectPageControlList = "SELECT [Id], [PageKey],[ControlID] FROM [PageControlList] WHERE  [Id]=@Id";

		private static readonly string SqlInsertPageControlList = "INSERT INTO [PageControlList]([PageKey],[ControlID]) values(@PageKey,@ControlID);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdatePageControlList = "UPDATE [PageControlList] SET [PageKey] = @PageKey,[ControlID] = @ControlID WHERE  [Id]=@Id";

		private static readonly string SqlDeletePageControlList = "DELETE [PageControlList] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllPageControlList = "SELECT [Id], [PageKey],[ControlID] FROM [PageControlList]";
		#endregion

		#region IPageControlListProvider Members
		public Core.Business.PageControlList Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectPageControlList);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.PageControlList pageControlList = new Core.Business.PageControlList();

				if (!reader.IsDBNull(0)) pageControlList.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) pageControlList.PageKey = reader.GetInt32(1);
				if (!reader.IsDBNull(2)) pageControlList.ControlID = reader.GetString(2);

				return pageControlList;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.PageControlList pageControlList)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@PageKey", SqlDbType.Int, pageControlList.PageKey);
			sql.AddParameter("@ControlID", SqlDbType.NVarChar, pageControlList.ControlID);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertPageControlList);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) pageControlList.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.PageControlList pageControlList)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@PageKey", SqlDbType.Int, pageControlList.PageKey);
			sql.AddParameter("@ControlID", SqlDbType.NVarChar, pageControlList.ControlID);
			sql.AddParameter("@Id", SqlDbType.Int, pageControlList.Id);
			sql.ExecuteSql(SqlUpdatePageControlList);
		}

		public void Delete(Core.Business.PageControlList pageControlList)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, pageControlList.Id);
			sql.ExecuteSql(SqlDeletePageControlList);
		}

		public IList<Core.Business.PageControlList> GetAllPageControlList()
		{
			IList<Core.Business.PageControlList> pageControlListlist = new List<Core.Business.PageControlList>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllPageControlList);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.PageControlList pageControlList = new Core.Business.PageControlList();

					if (!reader.IsDBNull(0)) pageControlList.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) pageControlList.PageKey = reader.GetInt32(1);
					if (!reader.IsDBNull(2)) pageControlList.ControlID = reader.GetString(2);

					pageControlList.MarkOld();
					pageControlListlist.Add(pageControlList);
				}
				reader.Close();
			}
			return pageControlListlist;
		}
		#endregion
	}
}
